package edu.tum.cs.i1.pse;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author arj
 *
 */
public class RealConnector implements ConnectorInterface {

	public InputStream open(URL url)
	{
		System.out.println("Getting resource through HTTP");
		try {
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
	 		con.setRequestMethod("GET");
			// Some web sites serve content based on the user agent and prevents the Java RE agent from getting proper response. Therefore this property is set.
			con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
			
			return con.getInputStream();
		} catch(Exception e) {}
		// If we hit this part something went wrong and we throw this (very informative) exception.
		System.out.println("Something went wrong. Either you are not connected to the internet, the specified URL was incorrect or something else that we don't want to handle in this demo.");
		return null;
	}
	
	
	public void close(InputStream stream) throws IOException
	{
		stream.close();
		System.out.println("RealConnector: connection closed");
	}
	
}
